package com.itheima.service.impl;

import com.itheima.DTO.SysMenuDTO;
import com.itheima.VO.MenuVO;
import com.itheima.entity.SysMenu;
import com.itheima.exception.SysMenuException;
import com.itheima.mapper.SysMenuMapper;
import com.itheima.service.SysMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor

public class SysMenuServiceImpl implements SysMenuService {

    private final SysMenuMapper sysMenuMapper;

    @Override
    public List<MenuVO> allData() {
        MenuVO parent = new MenuVO();
        parent.setId(0);
        List<SysMenu> sysMenuList = sysMenuMapper.selectAll();
        List<MenuVO> menuVOList = sysMenuList.stream().map(sysMenu -> sysMenuTransformToVO(sysMenu)).collect(Collectors.toList());
        getChildren(parent, menuVOList);
        return parent.getChildren();
    }
//        List<SysMenu> sysMenuList = sysMenuMapper.selectChildrenListByPid(0);
//
//        sysMenuList.forEach(sysMenu -> {
//            List<SysMenu> sysMenuList1 = sysMenuMapper.selectChildrenListByPid(sysMenu.getId());
//            sysMenu.setChildren(sysMenuList1);
//
//            sysMenuList1.forEach(sysMenu1 -> {
//                List<SysMenu> sysMenuList2 = sysMenuMapper.selectChildrenListByPid(sysMenu1.getId());
//                sysMenu1.setChildren(sysMenuList2);
//            });
//        });
//        return sysMenuList;

    @Override
    public void insertSysMenu(SysMenuDTO sysMenuDTO) {
//      Integer count = sysMenuMapper.selectSysMenuCountById(sysMenuDTO.getId());
        SysMenu sysMenu = copyToSysMenu(sysMenuDTO);
        sysMenuMapper.insertSysMenu(sysMenu);
    }

    @Override
    public void deleteSysMenu(Integer id) {
        sysMenuMapper.deleteSysMenuById(id);
    }

    @Override
    public MenuVO rowInfo(Integer id) {
        SysMenu sysMenu = sysMenuMapper.selectSysMenuById(id);
        MenuVO menuVO = sysMenuTransformToVO(sysMenu);
        return menuVO;
    }

    @Override
    public void update(SysMenuDTO sysMenuDTO) {
        sysMenuDTO.setUpdate_date(LocalDateTime.now());
        sysMenuMapper.updateSysMenuById(sysMenuDTO);
    }

    @Override
    public List<MenuVO> selectMenuByMenuId(List<Integer> menuIds) {
        List<MenuVO> menuVOList = sysMenuMapper.selectMenuByMenuId(menuIds);
        return menuVOList;
    }

    public void getChildren(MenuVO parent, List<MenuVO> menuVOList){
        List<MenuVO> children = new ArrayList<>();
        menuVOList.forEach(menuVO -> {
            if (Objects.equals(menuVO.getPid(), parent.getId())){
                children.add(menuVO);
            }
        });
        parent.setChildren(children);
        children.forEach(menuVO -> getChildren(menuVO, menuVOList));
    }

    private MenuVO sysMenuTransformToVO(SysMenu sysMenu) {
        MenuVO menuVO = new MenuVO();
        BeanUtils.copyProperties(sysMenu, menuVO);
        menuVO.setIs_show(sysMenu.getIsShow());
        menuVO.setIs_cache(sysMenu.getIsCache());
        menuVO.setIs_link(sysMenu.getIsLink());
        menuVO.setCreate_date(LocalDateTime.now());
        menuVO.setUpdate_date(LocalDateTime.now());
        menuVO.setDel_flag(sysMenu.getDelFlag());
        menuVO.setDefault_data(sysMenu.getDefaultData());
        return menuVO;
    }

    private SysMenu copyToSysMenu(SysMenuDTO sysMenuDTO) {
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuDTO, sysMenu);
        sysMenu.setIsShow(sysMenuDTO.getIs_show());
        sysMenu.setIsCache(sysMenuDTO.getIs_cache());
        sysMenu.setIsLink(sysMenuDTO.getIs_link());
        sysMenu.setUpdateDate(LocalDateTime.now());
        sysMenu.setCreateDate(LocalDateTime.now());
        sysMenu.setDelFlag(0);
        return sysMenu;
    }
}
